

function unusedElementFilter(element, index, array) {
	return (storageGet(element + '-displayed') != "1");
}

function getUnusedDefinitionsIds(){
	var nowTime = new Date().getTime();
	var unusedDefinitionIds;
	function unusedDefinitionFilter(element, index, array) {
		if(!storageGet(element + '-displayed') || storageGet(element + '-displayed') == ""){
			return true;
		}
		return (nowTime - storageGet(element + '-displayed')) > storageGet(element + '-frequency');
	}
	if(storageGet('definitionList') != ""){
		unusedDefinitionIds = storageGet('definitionList').split('|').filter(unusedDefinitionFilter);
	} else {
		unusedDefinitionIds = [];
	}
	return unusedDefinitionIds;
}

function init() {
    if(!storageGet('definitionList')){
		storageSet('definitionList', "");
	}
    if(!storageGet('frequency')){
		storageSet('frequency', DEFAULT_FREQUENCY);
	}
	
	chrome.extension.onRequest.addListener(
		function(request, sender, sendResponse) {
			var response;
			if(request.type == "save"){
				response = create(request);
			}
			else if(request.type == "getContent"){
				response = getContent();
			}
			else if(request.type == "updateMistakes"){
				response = updateMistakes(request);
			}
			else {
				response = {};
			}
			sendResponse(response);
		});
	synchronizeLoop();	
}

//message response
function updateMistakes(request){
	if(!storageGet(request.word + '-frequency')){
		storageSet(request.word + '-frequency', DEFAULT_FREQUENCY);
	}
	if(request.mistake){
		storageSet(request.word + '-frequency',  storageGet(request.word + '-frequency') / 2);
	} else {
		storageSet(request.word + '-frequency',  storageGet(request.word + '-frequency') * 2);
	}
	return {};
}

//message response
function create(request){
	var message = createCoreDefinition(request.word, request.definition);
	if(message && message != ""){
		return {message: message};
	} else {
		return {};
	}
}

//message response
function getContent(){
	var storedTime = storageGet('display-date');
	if(storedTime && storedTime != ""){
		var currentTime = new Date().getTime();
		if((currentTime - storedTime) < storageGet('frequency')){
			return {};
		} 
	}
	storageSet('display-date', currentTime);
			
	if(storageGet('definitionList') == ""){
		return {};
	}
	
	var unusedDefinitionIds = getUnusedDefinitionsIds();
	var wordId = getRandomNumber(unusedDefinitionIds.length);
	var word = unusedDefinitionIds[wordId];
	var definition = storageGet(word + '-definition');
	storageSet(word + '-displayed', new Date().getTime());
	var fake = getRandomNumber(4) == 0;
	if(fake){
		var words = storageGet('definitionList').split('|');
		words.splice(wordId,1);		
		var fakeWord = words[getRandomNumber(words.length)];
		var fakeDefinition = storageGet(fakeWord + '-definition').replace(fakeWord, word);
		return {word: word, definition: fakeDefinition, fake: true, realDefinition: definition};
	}
	return {word: word, definition: definition, fake: false, realDefinition: definition};	
}